﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using MapEntities;
using System.ComponentModel;
using ServerPlatform.Configuration;
using System.Configuration;

namespace WebControls
{
    public class SilverlightMap : WebControl
    {
        private static readonly string template = "<object data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='{0}' height='{1}'>" +
                                          "<param name='source' value='{2}'/>" +
                                          "<param name='Windowless' value='true'/>" +
                                          "<param name='onError' value='onSilverlightError' />" +
                                          "<param name='background' value='{3}' />" +
                                          "<param name='minRuntimeVersion' value='3.0.40624.0' />" +
                                          "<param name='autoUpgrade' value='true' />" +
                                          "<param name='initParams' value='{4}' />" +
                                          "<a href='http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0' style='text-decoration:none'>" +
                                          "<img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style:none'/></a>" +
                                          "</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0px;width:0px;border:0px'></iframe>";


        [Browsable(false)]
        public string RequestID
        {
            get
            {
                if (ViewState["RequestID"] != null)
                    return (string)ViewState["RequestID"];
                else return null;
            }

            private set
            {
                ViewState["RequestID"] = value;
            }
        }

        [Browsable(false)]
        public string PageUri
        {
            get
            {
                if (ViewState["PageUri"] != null)
                    return (string)ViewState["PageUri"];
                else if (Page != null)
                    return Page.Request.Url.AbsoluteUri.ToString();
                else return "";
            }

             set
            {
                ViewState["PageUri"] = value;
            }
        }

        private string ControlWidth
        {
            get
            {
                if (ViewState["CWidth"] == null)
                    return "";
                else return (string)ViewState["CWidth"];
            }
            set
            {
                ViewState["CWidth"] = value;
            }
        }

        private string ControlHeight
        {
            get
            {
                if (ViewState["CHeight"] == null)
                    return "";
                else return (string)ViewState["CHeight"];
            }
            set
            {
                ViewState["CHeight"] = value;
            }
        }

        [Browsable(false)]
        public bool IsDataBound
        {
            get
            {
                if (ViewState["bounded"] != null)
                    return true;
                else return false;
            }

            private set
            {
                if (value)
                    ViewState["bounded"] = true;
                else ViewState["bounded"] = null;
            }
        }



        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (IsDataBound)
            {
                string initParams = "RequestID=" + RequestID + ",pageUri=" + PageUri;
                //string source = "/ClientBin/VirtualEarth.xap";
                string source = "/map.axd?t=client";
                string width = ControlWidth;
                string height = ControlHeight;
                string background = "white";
                writer.Write(string.Format(template, width, height, source, background, initParams));
            }
            else
                base.Render(writer);
        }

        public void Bind(global::MapEntities.MapDataRequest request)
        {
            RequestID = request.Id.ToString();
            ControlWidth = request.MapProperties.Width;
            ControlHeight = request.MapProperties.Height;
            IsDataBound = true;

        }
    }
}
